package cn.fl.tag.service.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cmm.user.facade.system.dto.SysResourceDTO;
import cn.cmm.base.framework.context.ApplicationContextAccessor;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.UrlUtils;
import cn.lionbridge.center.context.CurrentThreadContext;
import cn.fl.system.service.ISysResourceService;

public class SysIndexMenuTag extends TagSupport {


	private static final long serialVersionUID = 7630202773940119631L;
	
	private static final Logger logger = LoggerFactory.getLogger(SysIndexMenuTag.class);
	
	/**
	 * 系统资源服务
	 */
	private ISysResourceService sysResourceService = (ISysResourceService)ApplicationContextAccessor.getBean("sysResourceServiceImpl");
	
	/**
	 * 资源集合
	 */
	private List<SysResourceDTO> list;
	
	/**
	 * JspWriter
	 */
	private JspWriter out;
	
	public void init(){
		out = pageContext.getOut();	
		try{
			list = this.sysResourceService.searchMenuWithAuthTree(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentRoleId());
		}catch(Error e){
			list = null;
		}
	}
	
	@Override
	public int doStartTag() throws JspException {
		this.init();
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		 try{
			 
			  /*<div id="accordion1">
	              <div title="客户管理-s" data-icon="sideNavico_24">
	                  <a class="l-link" data-method="f_addTab" tabid="client_21" title="客户列表" data-url="<%=path %>/customer/cstbscinfo/toListPage.do">客户列表</a>
	                  <a class="l-link" data-method="f_addTab" tabid="client_22" title="客户查询" data-url="<%=path %>/customer/cstbscinfo/toListPage.do">客户查询</a>
	              </div>
              </div>*/
			 String str= "";
			 if(CollectionUtils.isNotEmpty(list)){
				str="<div id=\"accordion1\">";
				for(int i=0;i<list.size();i++){
					SysResourceDTO obj=list.get(i);
					str+="<div title=\""+obj.getResNm()+"\" data-icon=\""+obj.getResImg()+"\">";
					if(CollectionUtils.isNotEmpty(obj.getChildList())){
						str+=returnStr(obj.getChildList());
					}
					str+="</div>";
				}
				str+="</div>";
			 }
			 out.write(str);
         } catch (IOException ex){
              throw new JspTagException("Error!");
         }
		 return EVAL_PAGE;
	}
	
	/***
	 *  递归遍历出所有结点的相关信息 
	 * @param s
	 * @return String
	 */
	public String returnStr(List<SysResourceDTO> child) {
		String str="";
		for(int i=0;i<child.size();i++){
			SysResourceDTO obj=child.get(i);
			String prefixUrl = UrlUtils.getRequestUrl(getRequest());
			prefixUrl = prefixUrl + obj.getResUrl();
			String tempResUrl = obj.getTempResUrl();
			if(StringUtils.isNotBlank(tempResUrl)){
				tempResUrl = tempResUrl+"&SQID="+CurrentThreadContext.getCurrentSQID();
			}
			str+="<a class=\"l-link\" data-method=\"f_addTab\" tabid=\"client_"+obj.getId()+"\" title=\""+obj.getResNm()+"\" temp-url=\""+tempResUrl+"\" data-url=\""+prefixUrl+"\">"+obj.getResNm()+"</a>";
			//str+="<div title=\""+obj.getResNm()+"\" data-icon=\"sideNavico_24\">";
			if(CollectionUtils.isNotEmpty(obj.getChildList())){
				str+=returnStr(obj.getChildList());
			}
			//str+="</div>";
		}
		return str;
	}

	
	/**
	 * 资源集合
	 */
	public List<SysResourceDTO> getList() {
		return list;
	}
	
	/**
	 * 资源集合
	 */
	public void setList(List<SysResourceDTO> list) {
		this.list = list;
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
}
